package gart.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.Socket;  
import java.nio.ByteBuffer;  
import java.nio.ByteOrder;  
import java.nio.CharBuffer;  
import java.nio.charset.CharacterCodingException;  
import java.nio.charset.Charset;  
import java.nio.charset.CharsetDecoder;  
import java.nio.charset.CharsetEncoder;  


public class Gan extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    
	
		PlayerData sendPd = new PlayerData();
		sendPd.setCharacterName("Assasin");
		sendPd.setPlayCardName("Sword");
		sendPd.setId(101);
		sendPd.setRoomID(10);
		sendPd.setHP(5);
		
		Charset charset = Charset.forName("UTF-8");  
		CharsetDecoder decoder = charset.newDecoder();  
		//CharsetEncoder encoder = charset.newEncoder();  
		
		try
		{
			Socket gart_Client = new Socket("st.thetigerparty.com",6666);
			
			OutputStream oStream = gart_Client.getOutputStream();
			InputStream iStream = gart_Client.getInputStream();
			
			DataOutputStream dos = new DataOutputStream(oStream);
			
			serializeToStream(dos,sendPd);
			
			DataInputStream dis = new DataInputStream(iStream);
			
			//receive new PlayerData
			PlayerData receivePD = new PlayerData();
			receivePD.setId(dis.readInt());
			receivePD.setRoomID(dis.readInt());
			
			//Receive CharacterName
			int length = dis.readInt();
			System.out.println("CharacterName" + length);
			byte[] stringBuffer = new byte[length];
			dis.read(stringBuffer);
			receivePD.setCharacterName(decoder.decode(ByteBuffer.wrap(stringBuffer)).toString());
			
			//Receive Play Card
			length = dis.readInt();
			System.out.println("PlayerCardName" + length);
			stringBuffer = new byte[length];
			dis.read(stringBuffer);
			receivePD.setPlayCardName(decoder.decode(ByteBuffer.wrap(stringBuffer)).toString());
			
			//Receive HP info
			receivePD.setHP(dis.readInt());
		
			System.out.println("PlayerID :" + receivePD.getId());
			System.out.println("RoomID :" + receivePD.getRoomID());
			System.out.println("CharacterName :" + receivePD.getCharacterName());
			System.out.println("PlayerCard :" + receivePD.getPlayCardName());
			System.out.println("Player HP :" + receivePD.getHP());
			System.out.flush();
			//
		}
		catch(Exception e)
		{
			e.printStackTrace(System.err);
		}
	}
	
	private void serializeToStream(DataOutputStream os, PlayerData pd) throws IOException  
	{  
	 // id  
	 os.writeInt(pd.getId()); 
	 // RoomID
	 os.writeInt(pd.getRoomID());	 	
	   
	 // length of CharacterName name  
	 
	 os.writeInt(pd.getCharacterName().getBytes("UTF-8").length);  
	 os.write(pd.getCharacterName().getBytes("UTF-8"));  
	 
	 // put PlayCardName  
	 
	 os.writeInt(pd.getPlayCardName().getBytes("UTF-8").length);  
	 os.write(pd.getPlayCardName().getBytes("UTF-8"));  
	   
	 // salary  
	 os.writeInt(pd.getHP());  
	}  
}